﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.Util;
using Model.Util.DTO;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;

namespace Repository.Util
{
   public static class UserOperateLogRep
    {
        public static List<UserOperateLogModel> GetMyBrowserHistory(this IRepositoryAsync<UserOprateLog> repository, int offset, int limit, string userId,int operateType)
        {
            string whereCondit = string.Format(@" where  1=1  and u.OprateType={0}", operateType);
            if (!string.IsNullOrEmpty(userId))
            {
                whereCondit += string.Format(@" and   u.UserId = '{0}' ", userId);
            }            
            string exeTotalSql = string.Format(@"
             declare   @TOTAL INT            
             select @TOTAL =count(u.LogID) from [WeiXin_BasicInfo].[dbo].[UserOprateLog] u        
                {0}
            ", whereCondit);

            string exeSql = string.Format(@"
      select * from ( 
              SELECT ROW_NUMBER() OVER(order by u.CreateTime desc) AS RowId,u.* 
                  ,@TOTAL AS Total,p.ThumbnailUrl1,p.ProductID,p.ProductName,p.SalePrice,p.Domain as IMGDomain
             from [WeiXin_BasicInfo].[dbo].[UserOprateLog] u    
             inner join WeiXin_BasicInfo.dbo.WeiXin_Product p
			 on u.RefID = p.ProductID                       
              {0}
            )  as b                      
         where RowId between {1} and {2} 
            ", whereCondit, (offset-1)*limit, offset * limit);
            return repository.SqlQueryT<UserOperateLogModel>(exeTotalSql + exeSql).ToList();
        }
    }
}
